package com.sun.study.forkjoin;

import static junit.framework.Assert.assertEquals;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import jsr166y.forkjoin.ForkJoinPool;

import org.junit.Test;

/**
 * Author: zhongxin
 * Created at: 2010-4-14 13:40:58
 */
public class CalculatorTest {
    
    @Test
    public void singleTask() throws InterruptedException, ExecutionException{
        
        System.out.println("syn:"+new SingleTaskCalculator(10).compute2());
        
        Thread.currentThread().sleep(20000);
        
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        Future<Long> result = forkJoinPool.submit(new SingleTaskCalculator(10));
        System.out.println("asyn:"+result.get());
       
        
      
    }
    
   
    public void run() throws Exception {

        long sum = 0;
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        for (int i = 0; i < 1000; i++) {
            long start = System.nanoTime();
            Future<Integer> result = forkJoinPool.submit(new Calculator(0, 10000));

            //assertEquals(new Integer(49995000), result.get());
            sum += System.nanoTime() - start;
        }

        System.out.println(sum / 1000);

        sum();
    }

    public void sum() {

        long sum = 0;

        for (int j = 0; j < 1000; j++) {
            long result = 0;
            long start = System.nanoTime();
            for (int i = 0; i < 10000; i++) {
                result += i;
            }
            sum += System.nanoTime() - start;
        }

        System.out.println(sum / 1000);
    }
}
